﻿function ShowModal(rendercontainerid, modalcontainerid, url) {
    if (url == '')
        return;
    $.ajaxSetup({ cache: false });
    $.get(url, function (data) {
        $(rendercontainerid).html(data);
        $(rendercontainerid).modal({
            close: false,
            containerId: modalcontainerid,
            onOpen: function (dialog) {
                dialog.overlay.fadeIn('slow', function () {
                    dialog.container.slideDown('slow', function () {
                        dialog.data.fadeIn('slow');
                    });
                });
            },
            onClose: function (dialog) {
                dialog.data.fadeOut('slow', function () {
                    dialog.container.slideUp('slow', function () {
                        dialog.overlay.fadeOut('slow', function () {
                            $.modal.close(); // must call this!
                        });
                    });
                });
            }
        });
    });
}

function CloseAndShowModal(rendercontainerid, modalcontainerid, url) {
    CloseDialog();
    ShowModal(rendercontainerid, modalcontainerid, url);
}


function CloseDialog() {
    $.modal.close();
}

function SubmitAndClose(rendercontainerid) {
    $(rendercontainerid).ajaxSubmit();
    CloseDialog();
    window.location = window.location;
}

function AjaxSubmitAndClose(formid) {
    var form = $(formid);
    $(form).validate();
    if (!$(form).valid())
        return;

    var options = {
        //dataType: 'json', // removed for jQuery 1.5 the ajaxSubmit has an open bug on it see: http://stackoverflow.com/questions/4957962/ajaxsubmit-in-combination-with-simplemodal-doesnt-fire-succes-delegate
        success: AjaxSubmitSucces
    };

    $(form).ajaxSubmit(options);
}

function AjaxSubmitSucces(data) {
    if (data.ErrorMessage != '') {
        alert(data.ErrorMessage);
    }
    else if (data.RedirectUrl != '') {
        CloseDialog();
        window.location = data.RedirectUrl;
    }
}